package com.flyco.modules.oms.handle.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.flyco.common.constant.YunDaStatus;
import com.flyco.common.util.HttpUtil;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.oms.entity.OmsOrderTrace;
import com.flyco.modules.oms.handle.ISyncLogisticsHandle;
import com.flyco.modules.oms.mapper.OmsOrderTraceMapper;
import com.flyco.modules.oms.model.OmsOrderYunDaTraceModel;
import com.flyco.modules.oms.service.IOmsOrderAdditionService;
import com.flyco.modules.util.ApiUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 同步韵达物流轨迹
 */
@Slf4j
@Component("logisticsHandle-韵达")
public class SyncLogisticsYunDaHandleImpl implements ISyncLogisticsHandle {
    @Autowired
    private OmsOrderTraceMapper omsOrderTraceMapper;
    @Autowired
    private IOmsOrderAdditionService omsOrderAdditionService;

    @Override
    public void syncLogistics(String billcode, String orderId) {
        log.info("开始同步韵达物流单号 :" + billcode);
        StringBuilder url = new StringBuilder();
        url.append(ApiUtil.getYundaUrl());
        url.append("query/json.php?partnerid=feikewuliu&mailno=");
        url.append(billcode);
        log.info("url:" + url.toString());
        String result = HttpUtil.getRequest(url.toString());
        /*String result = HttpUtil.createGet(url.toString())
                .contentType("application/json")
                .execute()
                .body();*/

        if (! oConvertUtils.isEmpty(result)) {
            JSONObject resultObj = JSON.parseObject(result);
            String status = resultObj.getString("status");
            JSONArray jsonArray = resultObj.getJSONArray("steps");
            log.info("jsonArray:" + jsonArray);
            if (null != jsonArray && jsonArray.size() > 0) {
                List<OmsOrderYunDaTraceModel> addModels = new ArrayList<>();
                Integer dbCount = omsOrderTraceMapper.getCountByTraceNo(billcode);
                List<OmsOrderYunDaTraceModel> models = jsonArray.toJavaList(OmsOrderYunDaTraceModel.class);
                Collections.sort(models, Comparator.comparing(OmsOrderYunDaTraceModel::getCreateTime));
                if (jsonArray.size() > dbCount) {
                    addModels = models.subList(dbCount, models.size());
                }
                //log.info("addModels:" + addModels);
                List<OmsOrderTrace> traces = new ArrayList<>();
                for (OmsOrderYunDaTraceModel traceModel : addModels) {
                    OmsOrderTrace trace = new OmsOrderTrace();
                    BeanUtils.copyProperties(traceModel, trace);
                    trace.setTraceNo(billcode);
                    trace.setLabel(YunDaStatus.fromName(traceModel.getLabel()).getStatusValue());
                    omsOrderTraceMapper.insert(trace);
                }

                if("signed".equalsIgnoreCase(status)) {
                    // TODO: 2020/4/8 处理订单签收
                    OmsOrderYunDaTraceModel traceModel =  models.get(models.size() - 1);
                    omsOrderTraceMapper.updateOrderSigned(orderId, billcode, traceModel.getCreateTime());

                    try {
                        omsOrderAdditionService.afterSign(orderId);
                    } catch (Exception e) {
                        log.error(e.getMessage());
                    }
                }
            }
        }
    }
}
